package com.qinggeng.project.system.controller;

import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.DistributionConfig;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.form.DistributionConfigForm;
import com.qinggeng.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 配送价格控制器
 *
 * @author WangKh
 * @date 2019-11-15
 */
@RestController
@RequestMapping("/system")
public class DistributionConfigController extends BaseController {

    @Autowired
    private DistributionConfigService distributionConfigService;
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrdersAddressService ordersAddressService;
    @Autowired
    private IOrdersCourierService ordersCourierService;
    @Autowired
    private IOrdersPointService ordersPointService;
    @Autowired
    private IShopService shopService;
    @Autowired
    private IUserLocationService userLocationService;


    @PreAuthorize("@ss.hasPermi('tool:distributionConfig:config')")
    @GetMapping(value = "/distributionConfig")
    public AjaxResult get(Long configId) {
        DistributionConfig distributionConfig = new DistributionConfig();
        distributionConfig.setRegionId(configId);
        return AjaxResult.success(distributionConfigService.queryDistributionConfigList(distributionConfig));
    }

//    @Log(title = "配送价格配置更新", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('tool:distributionConfig:config')")
    @PostMapping(value = "/distributionConfig")
    public AjaxResult update(@RequestBody DistributionConfigForm form) {
        return toAjax(distributionConfigService.createDistributionConfigAll(form.getDataList(),form.getRegionId()));
    }

    /**
     * 配送价格列表
     *
     * @param distributionConfig 查询条件
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:distributionConfig:list')")
    @GetMapping("/distributionConfig/list")
    public TableDataInfo list(DistributionConfig distributionConfig) {
        startPage();
        List<DistributionConfig> list = distributionConfigService.queryDistributionConfigList(distributionConfig);
        return getDataTable(list);
    }
    /**
     * 订单列表
     *
     * @param
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:distributionConfig:listOrders')")
    @GetMapping("/distributionConfig/listOrders")
    public AjaxResult listOrders(){
        startPage();
        Orders ordersWhere = new Orders();
        ordersWhere.setOrdersIndex(5);
        List<Orders> ordersList = ordersService.selectOrdersList(ordersWhere);
        for (Orders orders : ordersList) {
            orders.setOrdersAddress(ordersAddressService.selectOrdersAddressByOrdersId(orders.getOrdersId()));
            orders.setOrdersPoint(ordersPointService.selectOrdersPointByOrdersId(orders.getOrdersId()));
            orders.setCourier(ordersCourierService.selectOrdersCourierByOrdersId(orders.getOrdersId()));
            orders.setShop(shopService.selectShopById(orders.getShopId()));
        }
        return AjaxResult.success(ordersList);
    }


    @PreAuthorize("@ss.hasPermi('system:distributionConfig:locationInfo')")
    @GetMapping("/distributionConfig/locationInfo")
    public AjaxResult locationInfo(Long ordersId){
        return AjaxResult.success(userLocationService.selectUserLocationById(ordersId));
    }


    /**
     * 配送价格删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:distributionConfig:remove')")
    @Log(title = "配送价格删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/distributionConfig/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(distributionConfigService.deleteDistributionConfigById(id));
    }

}
